'use strict';
const path = require('path');

module.exports = function sub_plugin(md, page) {
    const defaultRender = md.renderer.rules.image;
    const videoRegExp = page.config.videoRegExp || /\.(mp4|ogv)$/;
    const audioRegExp = page.config.audioRegExp || /\.(mp3|ogg)$/;

    md.renderer.rules.image = function (tokens, idx, options, env, self) {
        const token = tokens[idx];
        const srcIndex = token.attrIndex('src');
        if (srcIndex > -1) {
            const videoIndex = token.attrIndex('video');
            const audioIndex = token.attrIndex('audio');
            let src = token.attrs[srcIndex][1];
            if (videoIndex > -1) {
                token.tag = 'video';
                token.attrs.splice(videoIndex, 1);
            } else if (audioIndex > -1) {
                token.tag = 'audio';
                token.attrs.splice(audioIndex, 1);
                console.log(token);
            } else {
                if (videoRegExp.test(src)) {
                    token.tag = 'video';
                } else if (audioRegExp.test(src)) {
                    token.tag = 'audio';
                }
            }
            // 如果src是以.开头的，则为相对路径，需要在当前page的路径下查找，否则在static查找
            if (src.startsWith('.')) {
                src = path.join(path.dirname(page.current.path), decodeURI(src));
                token.attrs[srcIndex][1] = src;
                if (page.current.copyResourceFile) {
                    page.current.copyResourceFile(src);
                }
            }
        }
        return defaultRender(tokens, idx, options, env, self);
    };
};
